package com.emep.changzhi.analyse.http.Technology;


import com.emep.changzhi.analyse.entity.Technology.Denitrification;
import com.emep.changzhi.analyse.entity.datamonitor.day.DataDayAirquality;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.Technology.DenitrificationService;
import com.emep.changzhi.analyse.utils.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("denitrification")
@RestController
@Api(value="脱销controller",tags={"脱销Cassandra"})
public class DenitrificationController {

    @Autowired
    private DenitrificationService service;

    /**
     * 脱销
     * @param payload
     * @return
     */
    @ApiOperation(value="脱销", notes="脱销")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @PostMapping(value = "add")
    public ResultBean<Denitrification> add(@RequestParam String payload){
        ResultBean result=null;
        if(payload != null || payload.length() != 0) {
            try {

                List<Map<String, Object>> list = GsonUtils.changeGsonToListMaps(payload);
                for (int i = 0; i < list.size(); i++) {
                    result = DataValidator.validateMapData(list.get(i), "value", "type", "companyId", "time");
                    if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
                        service.save(list.get(i));
                    }
                }
                result.setContent(list);
            } catch (Exception e) {
                log.error(e.getClass().getName()+":"+ e.getMessage());
                log.error(e.getLocalizedMessage());
                result=new ResultBean();
                result.setMsg("file");
                result.setCode(ResultBean.FAIL);
                result.setContent(payload);
            }
        }

        return  result;
    }
    /**
     *
     * @Description: 首页
     * @date 2018/3/8 11:49
     * @version V1.0
     */
    @ApiOperation(value="首页", notes="首页")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "查询数量", required = true, dataType = "String",paramType="query")
    })
    @GetMapping(value = "home")
    public List<Denitrification> findDataDayAirqualityByHome(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return service.findDataByHome(Integer.parseInt(payload.get("pageSize").toString()));
        }
        return  null;
    }
    /**
     *
     * @Description: 翻页查询
     * @date 2018/3/8 11:49
     * @version V1.0
     */
    @ApiOperation(value="翻页查询", notes="翻页查询")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询起始id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "查询数量", required = true, dataType = "Integer",paramType="query")
    })
    @GetMapping(value = "flip")
    public List<Denitrification> findDataDayAirqualityByFlip(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return service.findDataByFlip(payload.get("id").toString() , Integer.parseInt(payload.get("pageSize").toString()));
        }
        return null;
    }

}
